package upicgame.user.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import upicgame.user.dao.UserDao;
import upicgame.entity.User;
import upicgame.user.service.UserService;
import upicgame.utils.MD5Util;
import upicgame.utils.RegexUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: upicgame
 * @description: user服务实现类
 * @author: lhq
 * @create: 2019-12-17 14:33
 */
@Service(value = "userService")
@Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;


	@Override
	public User login(String username,String password) {
		//User user;
		User findUser = null;
		//正则表达式验证是否是邮箱
		if ( username != null && RegexUtils.isEmail(username)) {
			System.out.println("用户使用了邮箱登录");
			findUser = userDao.findUser(username,"email");
		}
		//正则表达式验证是否是用户名
		if (username != null && RegexUtils.isUsername(username)){
			System.out.println("用户使用了用户名");
			findUser = userDao.findUser(username,"name");
		}
		//正则表达式验证是否是手机号

		if ( username != null &&RegexUtils.isPhoneNumber(username)){
			System.out.println("用户使用了手机号");
			findUser = userDao.findUser(username,"phone");
		}


		if (findUser != null && password != null && MD5Util.validate(password,findUser.getPassword())) {
			System.out.println("密码正确!登录成功");
			return findUser;
		}else {
			System.out.println("密码错误!登录失败");
			return null;
		}


	}

	@Override
	public boolean updateUser(User user) {
		/**
		 * 获取并比对密码，如果密码未发生改变
		 * 则不进行md5加密，直接输入数据库
		 */
		User getOriginal = userDao.findUser(user.getId(),"id");
		if(! user.getPassword().equals(getOriginal.getPassword())){
			user.setPassword(MD5Util.encode2hex(user.getPassword()));
		}
		if(		! user.getName().equals(getOriginal.getName())
				&&RegexUtils.isUsername(user.getName())
				&& userDao.findUser(user.getName(),"name") != null){
			System.out.println("用户名发生了改变但用户名已存在");
		}
		if(! user.getEmail().equals(getOriginal.getEmail())
				&&RegexUtils.isEmail(user.getEmail())
				&& userDao.findUser(user.getEmail(),"email") != null){
			System.out.println("邮箱发生了改变,但被更改邮箱已存在");
		}
		if(! user.getPhone().equals(getOriginal.getPhone())
				&&RegexUtils.isPhoneNumber(user.getPhone())
				&& userDao.findUser(user.getPhone(),"phone") != null){
			System.out.println("手机号发生了改变,但被改变手机号已经存在");
		}

		return userDao.updateUser(user);
	}

	@Override
	public boolean addUser(User user) {
		System.out.println(RegexUtils.isUsername(user.getName()));
		System.out.println(userDao.findUser(user.getName(),"name") != null);
		if(  RegexUtils.isUsername(user.getName())  && userDao.findUser(user.getName(),"name") != null){
			System.out.println("该用户名已被注册");
			return false;
		}
		System.out.println(userDao.findUser(user.getName(),"email") != null);
		if ( RegexUtils.isEmail(user.getEmail()) && userDao.findUser(user.getEmail(),"email") != null){
			System.out.println("该邮箱已被注册");
			return false;
		}
		System.out.println(userDao.findUser(user.getName(),"phone") != null);
		if(RegexUtils.isPhoneNumber(user.getPhone()) && userDao.findUser(user.getPhone(),"phone") != null){
			System.out.println("该手机号已被注册");
			return false;
		}else {
			user.setPassword(MD5Util.encode2hex(user.getPassword()));
			return userDao.addUser(user);
		}


	}

	@Override
	public boolean deleteUser(Integer id){
		User user = userDao.findUser(id,"id");
		return userDao.deleteUser(user);
	}

	@Override
	public User findUser(Integer id,String idType) {
		return userDao.findUser(id,idType);
	}

	@Override
	public User findUser(String info,String infoType) {
		return userDao.findUser(info,infoType);
	}

	@Override
	public List<User> userLit(Integer currentPage,Integer size) {
		return userDao.findAllPage(currentPage,size);
	}

	@Override
	public Long totalCount() {
		return userDao.totalCount("User");
	}

	@Override
	public User addMoney(Integer id, Double money) {
		User user = userDao.findUser(id,"id");
		user.setMoney(money+user.getMoney());
		if(userDao.updateUser(user)){
			return user;
		}else {
			return null;
		}


	}
}
